package br.com.cdto.ldm;

import java.util.Comparator;

import org.primefaces.model.SortOrder;

import br.com.cdto.entity.Cornea;

public class LazySorterCornea implements Comparator<Cornea> {

    private String sortField;
    
    private SortOrder sortOrder;
    
    public LazySorterCornea(String sortField, SortOrder sortOrder) {
        this.sortField = sortField;
        this.sortOrder = sortOrder;
    }

    public int compare(Cornea car1, Cornea car2) {
        try {
        	int value = 0;
        	if(sortField.startsWith("id")){
        		value = ((Comparable)car1.getId()).compareTo(car2.getId());
        	}
        	if(sortField.startsWith("tipo")){
        		value = ((Comparable)car1.getTipo()).compareTo(car2.getTipo());
        	}
        	if(sortField.startsWith("status")){
        		value = ((Comparable)car1.getStatus()).compareTo(car2.getStatus());
        	}
           /* Object value1 = Cornea.class.getField(this.sortField).get(car1);
            Object value2 = Cornea.class.getField(this.sortField).get(car2);

            int value = ((Comparable)value1).compareTo(value2);*/
            
            return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 * value;
        }
        catch(Exception e) {
            throw new RuntimeException();
        }
    }
}
